package com.woniuxy.service.impl;

import com.woniuxy.dao.MessageDao;
import com.woniuxy.entity.Message;
import com.woniuxy.service.MessageService;
import com.woniuxy.utils.CodeUtils;
import com.woniuxy.utils.LoginRegister;
import com.woniuxy.utils.TimeUtils;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.woniuxy.dao.CoachDao;
import com.woniuxy.dao.MessageDao;
import com.woniuxy.dao.UserDao;
import com.woniuxy.dao.VenueDao;
import com.woniuxy.entity.Message;
import com.woniuxy.entity.Order;
import com.woniuxy.entity.PageBean;
import com.woniuxy.service.MessageService;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Data
@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageDao messageDao;

    @Autowired
    private VenueDao venueDao;

    @Autowired
    private CoachDao coachDao;

    @Autowired
    private UserDao userDao;


    /**
     * 查看所有发给我的消息
     * @param target 接收人id
     * @return 符合条件的所有消息
     */
    @Override
    public List<Message> findAllMessages(int target) {
        List<Message> allMessages = messageDao.findAllMessages(target);
        for (Message m :
                allMessages) {
            //循环获取他们的昵称
            if (m.getSender()< CodeUtils.COACH_INIT_CODE){
                m.setSendName(venueDao.findVenueByVenueId(m.getSender()).getNickname());
            }
            if (m.getSender()>= CodeUtils.COACH_INIT_CODE&&m.getSender()<CodeUtils.USER_INIT_CODE){
                m.setSendName(coachDao.findCoachByCid(m.getSender()).getNickname());
            }
            if (m.getSender()>= CodeUtils.USER_INIT_CODE){
                m.setSendName(userDao.findUserByUid(m.getSender()).getNickname());
            }
        }
        return allMessages;
    }

    /**
     * 添加新的消息
     * @param message 消息实例
     * @return int
     */
    @Override
    public int addNewMessage(Message message) {
        message.setSendTime(TimeUtils.getNowTime());
        if (messageDao.addNewMessage(message)!=0){
            return LoginRegister.OPERATION_SUCCESS;
        }
        return LoginRegister.OPERATION_FAILED;
    }

    /**
     * 通过id查通知消息
     * @param mid mid
     * @return null或者Message
     */
    @Override
    public Message findMessageByMid(int mid) {
        return messageDao.findMessageByMid(mid);
    }

    /**
     * 修改已读未读状态
     * @param mid 通知id
     * @return int
     */
    @Override
    public int changeReadStatus(int  mid) {
        Message messageByMid = findMessageByMid(mid);
        Map<String, Object> condition = new HashMap<>();
        condition.put("mid",mid);
        System.out.println("修改的这个通知的状态已读未读？："+messageByMid.getMstatus());
        switch (messageByMid.getMstatus()){
            case "1":condition.put("mstatus","0");break;
            case "0":condition.put("mstatus","1");break;
        }
        int i = messageDao.changeReadStatus(condition);
        if (i>0){
            return LoginRegister.OPERATION_SUCCESS;
        }
        return LoginRegister.OPERATION_FAILED;
    }

    /**
     * 修改签约状态
     * @param message  Rmstatus ，mid
     * @return int
     */
    @Override
    public int updateRmstatus(Message message) {
        int i = messageDao.updateRmstatus(message);
        if (i>0){
            return LoginRegister.OPERATION_SUCCESS;
        }
        return LoginRegister.OPERATION_FAILED;
    }

    @Override
    public int sendMessage(Message message) {
        messageDao.sendMessage(message);
        return 1;
    }
    @Override
    public PageBean<Message> getMessageByPage(int page, int target) {

        PageBean<Message> pageBean = new PageBean<Message>();
        int index = (page - 1) * 8;
        List<Message> data = messageDao.getMessageByPage(index, 8, target);
        // int total =coachDao.getNums();
        // System.out.println(total);
        //遍历集合
        Iterator<Message> iter=data.iterator();
        while (iter.hasNext()) {
            Message message = (Message) iter.next();
            System.out.println(message.getSender());
            int id=message.getSender();
            //得到信息发送方的id,根据id去查找信息
            if (id<100000) {
                message.setVenue(venueDao.findVenueByVenueId(id));
            }else if (id>100000&&id<10000000) {
                message.setCoach(coachDao.findCoachByCid(id));
            }else if (id>10000000) {
                message.setUser(userDao.findUserByUid(id));
            }


        }
        int totalNum =messageDao.getCountOfMessage(target);
        pageBean.setData(data);
        pageBean.setTotalNum(totalNum);
        pageBean.setTotalPage(totalNum % 8 == 0 ? totalNum / 8 : totalNum / 8 + 1);
        pageBean.setNumsOfCurrPage(data.size());
        pageBean.setPage(page);
        pageBean.setPageSize(8);
        return pageBean;
    }
    @Override
    public int changeMessageStatus(int mid) {
        messageDao.changeMessageStatus(mid);
        return 1;
    }


}